package com.bravo.rxevent;

/**
 * Created by bravo.lee on 2017/10/8.
 */

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.bravo.rxlib.Func;
import com.bravo.rxlib.Observable;
import com.bravo.rxlib.OnSubscriable;
import com.bravo.rxlib.Subscriable;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = "bravo";
    private Button btn;
    private ImageView ivImage;
    private String url = "A";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initEvent();
    }

    private void initEvent() {
        btn.setOnClickListener(this);
    }

    private void initView() {
        btn = (Button) findViewById(R.id.btn_init_image);
        ivImage = (ImageView) findViewById(R.id.imageView);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_init_image:
//                testBasic(url);
                testEventExchange(url);
//                testOnIo(url);
//                testMain(url);
                break;
            default:
                break;
        }
    }

    private void testMain(final String url) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                Observable.create(new OnSubscriable<String>() {
                    @Override
                    public void call(Subscriable subscriable) {
                        Log.i(TAG,"AA _ thread_name : "+Thread.currentThread().getName() + "thread_id : "+Thread.currentThread().getId());
                        /**
                         * do sthing get image url
                         */
                        subscriable.onNext(url);

                    }
                }).subscriableMain()
                        .subscriable(new Subscriable<String>() {
                            @Override
                            public void onNext(String s) {
                                Log.i(TAG,"BB _ thread_name : "+Thread.currentThread().getName() + "thread_id : "+Thread.currentThread().getId());
                            }
                        });
            }
        }).start();
    }

    private void testOnIo(final String url) {
        Observable.create(new OnSubscriable<String>() {
            @Override
            public void call(Subscriable subscriable) {
                Log.i(TAG,"AA _ thread_name : "+Thread.currentThread().getName() + "thread_id : "+Thread.currentThread().getId());
                /**
                 * do sthing get image url
                 */
                subscriable.onNext(url);

            }
        }).subscriableIo()
                .subscriable(new Subscriable<String>() {
                    @Override
                    public void onNext(String s) {
                        Log.i(TAG,"BB _ thread_name : "+Thread.currentThread().getName() + "thread_id : "+Thread.currentThread().getId());
                    }
                });
    }

    private void testEventExchange(final String url) {
        Observable.create(new OnSubscriable<String>() {
            @Override
            public void call(Subscriable subscriable) {
                subscriable.onNext(url);// MapSubcriable 的onNext方法。传入需要转换的参数。
            }
        }).map(new Func<String, Bitmap>() {
            /**
             * 将url转化为Bitmap  事件转换的具体实现。
             * @param url
             * @return 转换的结果。
             */
            @Override
            public Bitmap call(String url) {
                Toast.makeText(MainActivity.this, url, Toast.LENGTH_SHORT).show();
                return BitmapFactory.decodeResource(MainActivity.this.getResources(), R.mipmap.isport_logo);
            }
        }).subscriable(new Subscriable<Bitmap>() {
            @Override
            public void onNext(Bitmap bitmap) {
                ivImage.setImageBitmap(bitmap);
            }
        });
    }

    private void testBasic(final String url) {
        Observable.create(new OnSubscriable<String>() {
            @Override
            public void call(Subscriable subscriable) {
                /**
                 * do sthing get image url
                 */
                subscriable.onNext(url);
            }
        }).subscriable(new Subscriable<String>() {
            @Override
            public void onNext(String s) {
                Toast.makeText(MainActivity.this,s,Toast.LENGTH_SHORT).show();
            }
        });
    }
}
